#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"
#include "myinterface.h"

//主界面
HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);
    // 设置窗口标题
    setWindowTitle("全息家庭环境感知");

    //实例化六个界面
    myinterface = new Myinterface;
    m0Wig = new M0Wig;
    modbusWig = new ModbusWig;
    setWig = new SetWig;
    monitor = new Monitor;
    history = new History;

    //将六个界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(myinterface);//界面主题
    ui->stackedWidget->addWidget(m0Wig);//stm32
    ui->stackedWidget->addWidget(modbusWig);//modbus
    ui->stackedWidget->addWidget(setWig);//搜索界面
    ui->stackedWidget->addWidget(monitor);//监控界面
    ui->stackedWidget->addWidget(history);//历史显示界面

    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.50.66"));
    mqtt->setPort(1883);
    mqtt->connectToHost();


    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    //QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
    //modbusWig, SLOT(dataUpSlot(QByteArray)));

    //接收消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),modbusWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),m0Wig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),history, SLOT(dataUpSlot(QByteArray)));

    //通过客户端操作发送命令
    QObject::connect(modbusWig, SIGNAL(dataDownSignal(QMQTT::Message)),mqtt,SLOT(publish(QMQTT::Message)));
    QObject::connect(m0Wig, SIGNAL(dataDownSignal(QMQTT::Message)),mqtt,SLOT(publish(QMQTT::Message)));
    QObject::connect(history, SIGNAL(dataDownSignal(QMQTT::Message)),mqtt,SLOT(publish(QMQTT::Message)));
}

HomePage::~HomePage()
{
    delete ui;
}



